/**
 * Live GPS Tracking Application
 * Interactive map screen
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack.ui;

import nl.ivarclemens.gpstrack.GPSTrack;
import javax.microedition.lcdui.*;

public class MapUI extends Canvas implements CommandListener {

    private GPSTrack gpstrack = null;
    private MapWidget map = null;
    private Command backCommand = null;

    public MapWidget getMap()
    {
        return map;
    }

    public MapUI(GPSTrack gpstrack)
    {
        //super("Status");

        map = new MapWidget();
        map.setCanvas(this);

        this.gpstrack = gpstrack;

        //int mapWidth = Math.min(this.getWidth(), this.getHeight());

        map.setCentreMode(MapWidget.CENTRE_LATLON);
        map.setCentre(51.816946, 5.868845);
        map.setZoomLevel(16);

        map.setPreferredSize(this.getWidth(), this.getHeight());

        this.gpstrack.getNMEAParser().addPositionListener(map);

        backCommand = new Command("Back", Command.BACK, 2);
        this.addCommand(backCommand);
        this.setCommandListener(this);
    }

    protected void keyReleased(int keyCode) {
        if(keyCode == Canvas.KEY_NUM3)
            map.zoomIn();
        if(keyCode == Canvas.KEY_NUM9)
            map.zoomOut();
    }

    protected void paint(Graphics g)
    {
        map.paint(g, this.getWidth(), this.getHeight());
    }

    public void startTransmission()
    {
        map.startTransmission();
    }

    public void stopTransmission()
    {
        map.stopTransmission();
    }

    public boolean mayTransmit()
    {
        return map.mayTransmit();
    }

    public void commandAction(Command c, Displayable d)
    {
        if(c == backCommand)
            gpstrack.gotoScreen("menu");
    }
}
